package com.demo.springmvc.controller;

import com.demo.springmvc.common.domain.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

@Controller
public class UserController {
    //非ajax 方式，刷新方式
    @RequestMapping("/login1")
    public String login(ModelMap modelMap,User user){
        if(user.getName().equals("admin") && user.getPassword().equals("admin")){
            return "success";
        }else{
            modelMap.put("msg","用户名或密码错误");
            return "login";
        }
    }

    @RequestMapping("/login2")
    //加上这个注解后
//    @RequestBody  专门接受 "application/json"方式请求
    @ResponseBody
    public Object login(@RequestBody User user){
        Map<String,Object> map = new HashMap<>();
        if(user.getName().equals("admin") && user.getPassword().equals("admin")){
            map.put("success",true);
        }else{
            map.put("success",false);
            map.put("msg","用户名或密码错误");
        }
        return map;
    }

}
